import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:paly_android/ui/home_page.dart';
import 'package:paly_android/ui/login_page.dart';
import 'package:paly_android/ui/main_page.dart';
import 'package:paly_android/ui/project_tree_list.dart';
import 'package:paly_android/ui/register_page.dart';
import 'package:paly_android/ui/webview_page.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyRouter {

  static final String LOGIN="/login";
  static final String REGISTER="/register";
  static final String MAIN="/main";
  static final String WEB="/web";
  static final String PTL="/ptl";



  static final routes = {
    LOGIN: (BuildContext context, {arguments}) => LoginPage(),
    REGISTER: (BuildContext context, {arguments}) => RegisterPage(),
    MAIN: (BuildContext context, {arguments}) => MainPage(),
    WEB: (BuildContext context, {arguments}) => WebViewPage(arguments),
    PTL: (BuildContext context, {arguments}) => ProjectTreeList(),
  };

  // 处理参数传递

  static final RouteFactory onGenerateRoute = (RouteSettings settings) {
    // 获取声明的路由页面函数
    LogUtil.e("onGenerateRoute settings.name ${settings.name}");

    var pageBuilder = routes[settings.name];

    if (pageBuilder != null) {
      if (settings.arguments != null) {
        // 创建路由页面并携带参数
        return MaterialPageRoute(
            builder: (context) =>
                pageBuilder(context, arguments: settings.arguments),
            settings: RouteSettings(
                name: settings.name, arguments: settings.arguments));
      } else {
        return MaterialPageRoute(builder: (context) => pageBuilder(context));
      }
    }
    return MaterialPageRoute(
        builder: (context) => Text("未知页面${settings.name}"));
  };
}
